/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 * */

// AmzServer - Tototoy's server model.
// Author: tonyjobmails@gmail.com (tonyhack).
//
// Ring queue's common define.
//

#ifndef __RING__QUEUE__DEFINE
#define __RING__QUEUE__DEFINE

#include "common.h"
#include "logging.h"

namespace amz {

namespace ring_queue_define {

enum RingQueueType {
  RingQueueType_SingleBound = 0,
  RingQueueType_SingleBoundless, 
  RingQueueType_MultipleBound,
  RingQueueType_MultipleBoundless,

  RingQueueType_Max,
};

// A configure to construct a ring queue.
struct RingQueueConfigure {
  RingQueueType type_;
  uint32 number_of_producers_;
  uint32 ring_size_;
  uint32 dynamic_queue_size_;
  bool blocking_;
};

// The default size of ring queue.
const uint32 kDefaultRingQueueSize = 1024 * 1024 * 32;

// check is empty.
#define RING_QUEUE_EMPTY(read_pointer, write_pointer) \
  ((read_pointer) == (write_pointer))
  
// check is full.
#define RING_QUEUE_FULL(max_size, read_pointer, write_pointer) \
  (((write_pointer + 1) % (max_size + 1)) == (read_pointer))
  
// get free size.
#define RING_QUEUE_FREE_SIZE(max_size, read_pointer, write_pointer, free_size) \
  do { \
    if(RING_QUEUE_EMPTY(read_pointer, write_pointer)) { \
      free_size = max_size; \
    } else if(read_pointer > write_pointer) { \
      free_size = read_pointer - write_pointer - 1; \
    } else { \
      free_size = max_size - (write_pointer - read_pointer); \
    } \
  } while(false);
  
// This is some error
// The size after write_pointer.
#define RING_QUEUE_FREE_SIZE_AFTER_WRITE_POINTER(max_size, read_pointer, write_pointer, free_size_after_write_pointer) \
  do { \
    if(read_pointer == write_pointer) \
      free_size_after_write_pointer = max_size - write_pointer + 1;\
    else if(read_pointer > write_pointer) \
      free_size_after_write_pointer = read_pointer - write_pointer - 1; \
    else \
      if(read_pointer == 0) \
        free_size_after_write_pointer = max_size - write_pointer; \
      else \
        free_size_after_write_pointer = max_size - write_pointer + 1; \
  } while(false);
  
// The size you can read from ring queue.
#define RING_QUEUE_READ_SIZE(max_size, read_pointer, write_pointer, read_size) \
  do { \
    RING_QUEUE_FREE_SIZE(max_size, read_pointer, write_pointer, read_size) \
    read_size = max_size - read_size; \
  } while(false);
  
// The read size after read_pointer.
#define RING_QUEUE_READ_SIZE_AFTER_READ_POINTER(max_size, read_pointer, write_pointer, read_size_after_read_pointer) \
  do { \
    if(write_pointer == read_pointer) \
      read_size_after_read_pointer = 0; \
    else if(write_pointer > read_pointer) \
      read_size_after_read_pointer = write_pointer - read_pointer; \
    else \
      read_size_after_read_pointer = max_size - read_pointer + 1; \
  } while(false)\

}  // namespace ring_queue_define

}  // namespace amz

#endif  // __RING__QUEUE__DEFINE

